<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/18
 * Time: 21:38
 */

namespace App\HttpController\V1;


use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Response\QrCodeResponse;

class Share extends BaseWithDb
{
//    protected $web_url='';
    /**
     * 文章分享
     * input:user_id 用户ID  study_id
     */
    public function text_share()
    {
        $input = $this->input();
        try {
//            $user=$this->db
//                ->where('uuid', $input['uuid'])->getValue('id');
            $list = $this->db
                ->where('id', $input['study_id'])
                ->getOne('study', 'id,study_title,user_video,time,share_text,browse,`like`,favorite,comm_num,create_time');
            $user = $this->db->where('id', $input['user_id'])->getOne('users', 'qrcode_url,nickname,mobile,sign');
            if ($user['qrcode_url'] == '') {
                $code=$this->code($this->db,$input['user_id']);
                $user['qrcode_url']=$code['img'];
            }
            $user['qrcode_url']=$this->api_url.$user['qrcode_url'];
            return $this->writeJson(200, ['list' => $list, 'user_info' => $user]);
        } catch (\Exception $exception) {
            return $this->writeJson($exception->getCode(), '', $exception->getMessage());
        }
    }

    /**
     * 视频分享
     * input:user_id 用户ID study_id 详情ID
     */
    public function video_share()
    {
        $input = $this->input();
        try {
              $snow= new Snow();
              $snow->browseinc($this->db,$input['study_id']);
//            $user=$this->db
//                ->where('uuid', $input['uuid'])->getValue('id');
            $table_name = '`study` as b';
            $list = $this->db
                ->where('b.id', $input['study_id'])
                ->join('`study_author` as a', 'a.id = b.author_id', 'LEFT')
                ->get($table_name, null, 'b.id,b.study_title,b.content,b.share_text,a.id as uid,b.vip_video,b.introduce,b.image,b.create_time,b.user_id,b.time,browse');
            $introduce='';
            foreach ($list as $k => &$v) {
                $v['image'] = ($v['image'] != '') ? $this->video_url . $v['image'] : '';
                $v['vip_video'] = ($v['vip_video'] != '') ? $this->video_url . $v['vip_video'] : '';
                $user_id = $v['uid'];
                $introduce=strip_tags($v['introduce']);
            }
            #名师资料
            if (isset($user_id) && $user_id > 0) {
                $user = $this->db->where('id', $user_id)->getOne('study_author');
                $user['headimg']=$this->video_url.$user['headimg'];
                $user['author_introduce']=strip_tags($user['author_introduce']);
            } else {
                $user = [];
            }

            $users = $this->db->where('id', $input['user_id'])->getOne('users', 'nickname,uuid,mobile,sign,qrcode_url');
//            $users['qrcode_url']=$this->video_url.$users['qrcode_url'];
            if ($users['qrcode_url'] == '') {
                $code=$this->code($this->db,$input['user_id']);
                $users['qrcode_url']=$code['img'];
                $users['url']=$code['url'];
//                $users['qrcode_url']=$this->api_url.$users['qrcode_url'];
            }else{
                $users['qrcode_url']=$this->api_url.$users['qrcode_url'];
            }
            return $this->writeJson(200, ['list' => $list, 'userinfo' => $user, 'user_info' => $users]);
        } catch (\Exception $exception) {
            return $this->writeJson($exception->getCode(), '', $exception->getMessage());
        }
    }


    public function code($model, $user_id)
    {
        $user_info = $model->where('id', $user_id)->getOne('users');
        $uid = $user_id;
        $register_url = $this->web_url . '/visitors/register?uuid=' . $user_info['uuid'];
//        if ($user_info['qrcode_url']!='' && $user_info['qrcode_urls']!='' ){
//            $data['img']=$this->url.$user_info['qrcode_url'];#
//            $data['url']=$register_url;
//        }
        $qrCode = new QrCode($register_url);
        $qrCode->setSize(300);
        $qrCode->setWriterByName('png');
        $qrCode->setMargin(5);
        $qrCode->setEncoding('UTF-8');
        $qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
//        $qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER);
//        $qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
        $qrCode->setLogoSize(150, 200);
        $qrCode->setRoundBlockSize(true);
        $qrCode->setValidateResult(false);
        $qrCode->setWriterOptions(['exclude_xml_declaration' => true]);
//        header('Content-Type: '.$qrCode->getContentType());
//        $qrCode->writeFile(EASYSWOOLE_ROOT.'/Upload/usersimg/qrcode/short/qrcode'.$uid.'.png');
        $qrCode->writeFile(EASYSWOOLE_ROOT . '/Upload/usersimg/qrcode' . $uid . '.png');
//        $long=$this->h_code($uid,'',$user_info['uuid'],$user_info['uuid']);
//        $short=$this->long_code($uid,'',$user_info['uuid'],$user_info['uuid']);
//        return $this->writeJson(200,$long);
        $img = '/Upload/usersimg/qrcode' . $uid . '.png';#
//        return $this->writeJson(200, $user_info, '获取成功');

        $data['url'] = $user_info['uuid'];
        $data['img'] = $this->api_url . $img;
        #二维码存入数据库
        $model->where('id', $user_id)->update('users', ['qrcode_url' => $img]);
//        return $this->writeJson( 200, $data, '获取成功');
        return $data;
    }
}